#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QKeyEvent>
#include <QCoreApplication>
#include <QRandomGenerator>
#include <QMessageBox>
extern QString token;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_ptrFoodSql(nullptr)
{
    ui->setupUi(this);

    QFile fcss;
    fcss.setFileName(":/loginpage.css");
    fcss.open(QIODevice::ReadOnly);
    QString strQss = fcss.readAll();
    m_loginPage.setStyleSheet(strQss);

    fcss.close();
    fcss.setFileName(":/mainwindow.css");
    fcss.open(QIODevice::ReadOnly);
    strQss = fcss.readAll();
    this->setStyleSheet(strQss);



    m_loginPage.show();//显示登录页
    auto f = [&](){
      this->show();
      //qDebug()<<token;
    };
    connect(&m_loginPage,&loginPage::sendLoginSuccess,this,f);//收到发送成功的信号就引用f

    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(1);//设置多少列
    QStringList l;
    l<<"管理您的冰箱";
    QTreeWidgetItem *p = new QTreeWidgetItem(ui->treeWidget,l);
     ui->treeWidget->addTopLevelItem(p);

    l.clear();
    l<<"菜谱推荐";
    QTreeWidgetItem *p1 = new QTreeWidgetItem(p,l);
    auto f1 = [&](){
      qDebug("显示菜谱推荐");
      m_recommend.show();
    };
    connect(ui->treeWidget,&QTreeWidget::itemClicked,this,f1);

    l.clear();
    l<<"变质提醒";
    QTreeWidgetItem *p2 = new QTreeWidgetItem(p,l);

    p->addChild(p1);
    p->addChild(p2);

    ui->treeWidget->expandAll();//默认全部展开
    ui->stackedWidget->setCurrentIndex(0);//默认显示的页面

    m_ptrFoodSql = foodSql::getinstance();

   updateTable();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_F6)
    {
        QFile f;
        auto str = QCoreApplication::applicationDirPath();
        f.setFileName(str+"//"+"stuqss.css");
        f.open(QIODevice::ReadOnly);
        QString strqss = f.readAll();
        this->setStyleSheet(strqss);
        m_loginPage.setStyleSheet(strqss);
    }
}


void MainWindow::on_exitBtn_clicked()
{
    exit(0);
}


void MainWindow::on_btn_simulation_clicked()
{
    //模拟10个食物数据
    QRandomGenerator r;
    r.seed(0);
    for(int i=0;i<10;i++)
    {
        FoodInfo info;
        auto num = r.bounded(1,100);
        auto foodid=r.bounded(100,200);
        if(i%3==1)
        {
            info.name=QString("菜%1").arg(i);
        }
        else if(i%3==2)
        {
            info.name=QString("白%1").arg(i);
        }
        else
        {
            info.name=QString("大%1").arg(i);
        }
        info.num=num;
        info.foodid=foodid;
        info.foodunit=QString("个");
        info.date=QString("模拟日期");
        m_ptrFoodSql->addFood(info);
    }

}


void MainWindow::on_btn_add_clicked()
{
    m_diaAddFood.setType(true);
    m_diaAddFood.exec();
    updateTable();
}


void MainWindow::on_btn_clear_clicked()
{
    m_ptrFoodSql->clearFoodTable();
    updateTable();
}

void MainWindow::updateTable()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(7);
    QStringList l;
    l<<"序号"<<"id"<<"名称"<<"数量"<<"单位"<<"时间"<<"建议保质期";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    //设置只能选中行
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    //ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//设置能不能同时选中多行，选中多行触摸屏怎么搞？触摸屏应该不需要这个，去掉注释就只能单行选中了
    //设置不能编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt = m_ptrFoodSql->getFoodCut();
    ui->lb_cnt->setText(QString("数量:%1").arg(cnt));
    QList<FoodInfo> lFoods = m_ptrFoodSql->getPageFood(0,cnt);

    //ui->tableWidget->clear();
    ui->tableWidget->setRowCount(cnt);
    for(int i=0;i<lFoods.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(lFoods[i].id)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(lFoods[i].name));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(lFoods[i].num)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(lFoods[i].foodunit));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(lFoods[i].date));
    }
}


void MainWindow::on_btn_delete_clicked()
{
    int n = ui->tableWidget->currentRow();
    if(n>=0)
    {
        int id = ui->tableWidget->item(n,1)->text().toUInt();
        m_ptrFoodSql->delFood(id);
        QMessageBox::information(nullptr,"信息","删除成功");
        updateTable();
    }
}


void MainWindow::on_btn_update_clicked()
{
    FoodInfo info;
    int i = ui->tableWidget->currentRow();
    if(i>=0)
    {
        info.id=ui->tableWidget->item(i,1)->text().toUInt();
        info.name=ui->tableWidget->item(i,2)->text();
        info.num=ui->tableWidget->item(i,3)->text().toUInt();
        info.foodunit=ui->tableWidget->item(i,4)->text();
        info.date=ui->tableWidget->item(i,6)->text();
        m_diaAddFood.setType(false,info);
        m_diaAddFood.exec();
    }

    updateTable();
}


void MainWindow::on_btn_search_clicked()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(7);
    QStringList l;
    l<<"序号"<<"id"<<"名称"<<"数量"<<"单位"<<"时间"<<"建议保质期";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    //设置只能选中行
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    //ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//设置能不能同时选中多行，选中多行触摸屏怎么搞？触摸屏应该不需要这个，去掉注释就只能单行选中了
    //设置不能编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt = m_ptrFoodSql->getFoodCut();
    ui->lb_cnt->setText(QString("数量:%1").arg(cnt));
    QList<FoodInfo> lFoods = m_ptrFoodSql->getPageFood(0,cnt);

    QString strFilter=ui->le_search->text();
    //ui->tableWidget->clear();
    ui->tableWidget->setRowCount(cnt);
    int index = 0;
    for(int i=0;i<lFoods.size();i++)
    {
        if(!lFoods[i].name.contains(strFilter))
        {
            continue;
        }
        ui->tableWidget->setItem(index,0,new QTableWidgetItem(QString::number(index+1)));
        ui->tableWidget->setItem(index,1,new QTableWidgetItem(QString::number(lFoods[i].id)));
        ui->tableWidget->setItem(index,2,new QTableWidgetItem(lFoods[i].name));
        ui->tableWidget->setItem(index,3,new QTableWidgetItem(QString::number(lFoods[i].num)));
        ui->tableWidget->setItem(index,4,new QTableWidgetItem(lFoods[i].foodunit));
        ui->tableWidget->setItem(index,6,new QTableWidgetItem(lFoods[i].date));
        index++;
    }
}

